<template>
    <div>
        <avue-form ref="form" v-model="obj" :option="option"
                   @reset-change="emptytChange"
                   @submit="submit">

        </avue-form>
    </div>
</template>

<script>

import {uploadPath} from "@/config/env";

export default {
    data() {
        return {
            obj: {},
            treeData: [],
            isIdFind: true,
            initSuccess: false,
            levelDict: [
                {
                    label: "省/自治区/直辖市",
                    value: 0
                },
                {
                    label: "市级",
                    value: 1
                },
                {
                    label: "县级",
                    value: 2
                }
            ],
        }
    },
    props: {
        uri: {},
        rowData: {},
    },
    computed: {
        option() {
            return {
                // size: "small",
                submitBtn: true,
                emptyBtn: true,
                submitText: '提交',
                emptyText: "关闭",
                column: [
                    {
                        label: '父级',
                        prop: 'pid',
                        type: 'tree',
                        labelWidth: 110,
                        span: 20,
                        display: true,
                        dicData: this.treeData,
                        props: {
                            value: "id",
                            label: "name",
                            children: "children"
                        },
                        rules: [{
                            required: true,
                            message: "请选择 父级id",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: '区划名称',
                        prop: 'name',
                        labelWidth: 110,
                        maxlength: 32,
                        showWordLimit: true,
                        span: 20,
                        rules: [{
                            required: true,
                            message: "请输入 区划名称",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: '区划编码',
                        prop: 'code',
                        labelWidth: 110,
                        maxlength: 32,
                        showWordLimit: true,
                        span: 20,
                        rules: [{
                            required: false,
                            message: "请输入 区划编码",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: '备注',
                        prop: 'remark',
                        labelWidth: 110,
                        maxlength: 64,
                        showWordLimit: true,
                        span: 20,
                        rules: [{
                            required: false,
                            message: "请输入 备注",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: '层级',
                        prop: 'level',
                        type: "select",
                        dicData: this.levelDict,
                        labelWidth: 110,
                        maxlength: 9,
                        showWordLimit: true,
                        span: 20,
                        rules: [{
                            required: true,
                            message: "请输入 级次id 0",
                            trigger: "blur"
                        }]
                    },

                ]
            }
        }
    },
    created() {
        this.treeData = JSON.parse(JSON.stringify(this.rowData.pidDic));
        this.treeDisabledHandle(this.treeData, this.rowData.id);
        if (this.isIdFind) {
            this.findId(this.rowData);
        } else {
            this.obj = this.rowData;
            this.initSuccess = true;
        }
    },
    methods: {
        emptytChange() {
            this.$emit('closeDialog', false);
        },
        // 递归处理自己 + 自己的下级ids 不能选
        treeDisabledHandle(children, id) {
            if (children == null) {
                return
            }
            for (let i = 0; i < children.length; i++) {
                if (children[i].pid === id || children[i].id === id) {
                    children[i].disabled = true;
                    this.treeDisabledHandle(children[i].children, children[i].id)
                } else {
                    this.treeDisabledHandle(children[i].children, id);
                }
            }
        },
        findId(rowData) {
            if (rowData != null && rowData.id != null) {
                this.crud.get(this.uri.info + "/" + rowData.id).then((res) => {
                    Object.assign(this.obj, res.data.data);
                    this.initSuccess = true;
                })
            }
        },
        submit(form, done) {
            this.crud.put(this.uri.info + "/" + this.obj.id, this.obj).then((res) => {
                if (res.data.code == 200) {
                    this.$emit('closeDialog', true, this.obj.id);
                }
                done(form);
            }).catch(err => {
                console.error(err);
                done(form);
            })
        },
    }
}
</script>
